﻿@using eLoan.Models;
@model PrintChequeModel

@{
    ViewBag.Title = "Print Cheque";
}

<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2>Print Cheque</h2>
        <ol class="breadcrumb">
            <li>
                Funding
            </li>
            <li class="active">
                <strong>Print Cheque</strong>
            </li>
        </ol>
    </div>
    <div class="col-lg-2"></div>
</div>


<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
            @using (Html.BeginForm("PrintCheque", "Funding", FormMethod.Post, new { id = "saveForm" }))
            {
                @Html.AntiForgeryToken()
                @Html.ValidationSummary(true)
                
                <fieldset>
                    <div class="ibox-title">
                        <div class="divSearch">
                            <table class="table-search">
                                <tr>
                                    <td class="tdFixed">
                                        <span>@Html.LabelFor(m => m.Branches)</span>
                                        @Html.DropDownListFor(m => m.SelectedBranchCode, Model.Branches, new { @id = "ddlBranches", @class = "text-box single-line" })
                                    </td>
                                    <td class="tdFixed">
                                        @Html.TextBoxFor(m => m.SearchText, new { @class = "single-line" })
                                    </td>
                                    <td class="tdFixed">
                                        <button type="submit" id="btnRefresh" class="btn btn-primary btnSearch" name="command" value="Refresh">Search</button>
                                    </td>
                                    <td class="tdAlignRight">
                                        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#confirm-save">Save as printed</button>
                                        <button type="button" id="btnSave" style="display:none" onclick="ValidateFields()">Validate</button>
                                        <button type="submit" id="btnSubmit" style="display:none" name="command" value="Save">Submit</button>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                    <div class="ibox-title">
                        <h3><span>Note: Cheque Amount = Loan Amount - Total Deduction</span></h3>
                    </div>
                    <div class="ibox-content">
                        <div class="divScroll">
                            <table id="tbl" class="table table-bordered">
                                <thead class="k-grid-header">
                                    <tr>
                                        <th class="k-header" style="width:10%">
                                            <span class="k-link">Document No</span>
                                        </th>
                                        <th class="k-header" style="width:10%">
                                            <span class="k-link">Borrower</span>
                                        </th>
                                        <th class="k-header" style="width:10%">
                                            <span class="k-link">Agent</span>
                                        </th>
                                        <th class="k-header" style="width:10%">
                                            <span class="k-link">Loan Amount</span>
                                        </th>
                                        <th class="k-header" style="width:10%">
                                            <span class="k-link">Loan Interest %</span>
                                        </th>
                                        <th class="k-header" style="width:10%">
                                            <span class="k-link">Receivable</span>
                                        </th>
                                        <th class="k-header" style="width:10%">
                                            <span class="k-link">Total Deduction</span>
                                        </th>
                                        <th class="k-header" style="width:10%">
                                            <span class="k-link">Bank</span>
                                        </th>
                                        <th class="k-header" style="width:10%">
                                            <span class="k-link">Cheque No</span>
                                        </th>
                                        <th class="k-header" style="width:10%">
                                            <span class="k-link">Cheque Amount</span>
                                        </th>
                                        <th colspan="3" style="text-align:center">
                                            @Html.CheckBox("chkCheckAll", false, new { @class = "checkAll", @onchange = "checkAll(this)" })
                                        </th>
                                    </tr>
                                </thead>
                                <tbody id="tableBody">
                                    @for (var i = 0; i < Model.PrintChequeItems.Count(); i++)
                                    {
                                    <tr class="trData">
                                        <td>
                                            @Html.ActionLink(Model.PrintChequeItems[i].DocumentNo, "LoanDetails", "Loan", null, null, null, new { documentNo = Model.PrintChequeItems[i].DocumentNo }, null)
                                            @Html.HiddenFor(m => m.PrintChequeItems[i].DocumentNo)
                                        </td>
                                        <td>
                                            @Html.ValueFor(m => m.PrintChequeItems[i].BorrowerName)
                                            @Html.HiddenFor(m => m.PrintChequeItems[i].BorrowerName)
                                        </td>
                                        <td>
                                            @Html.ValueFor(m => m.PrintChequeItems[i].AgentName)
                                            @Html.HiddenFor(m => m.PrintChequeItems[i].AgentName)
                                        </td>
                                        <td style="text-align:right">
                                            @Html.ValueFor(m => m.PrintChequeItems[i].LoanAmount, "{0:#,###,##0.00}")
                                            @Html.HiddenFor(m => m.PrintChequeItems[i].LoanAmount)
                                        </td>
                                        <td style="text-align:right">
                                            @Html.ValueFor(m => m.PrintChequeItems[i].LoanInterest, "{0:#,###,##0.00}")
                                            @Html.HiddenFor(m => m.PrintChequeItems[i].LoanInterest)
                                        </td>
                                        <td style="text-align:right">
                                            @Html.ValueFor(m => m.PrintChequeItems[i].Receivable, "{0:#,###,##0.00}")
                                            @Html.HiddenFor(m => m.PrintChequeItems[i].Receivable)
                                        </td>
                                        <td style="text-align:right">
                                            @Html.ValueFor(m => m.PrintChequeItems[i].TotalDeduction, "{0:#,###,##0.00}")
                                            @Html.HiddenFor(m => m.PrintChequeItems[i].TotalDeduction)
                                        </td>
                                        <td>
                                            @Html.DropDownListFor(m => m.PrintChequeItems[i].BankID, Model.Banks, new { @class = "single-line" })
                                        </td>
                                        <td>
                                            @Html.TextBoxFor(m => m.PrintChequeItems[i].ChequeNo, new { @id = "txtChequeNo_" + Model.PrintChequeItems[i].DocumentNo, @class = "single-line scroll-field1" })
                                            <div id="valChequeNo_@Model.PrintChequeItems[i].DocumentNo" style="display:none">Cheque number is required.</div>
                                        </td>
                                        <td style="text-align:right">
                                            @Html.TextBoxFor(m => m.PrintChequeItems[i].ChequeAmount, "{0:#,###,##0.00}", new { @onchange = "FormatMe(this);", @class = "single-line scroll-field2" })
                                            <div id="valChequeAmount_@Model.PrintChequeItems[i].DocumentNo" style="display:none">Cheque amount is required.</div>
                                        </td>
                                        <td>
                                            @Html.CheckBoxFor(m => m.PrintChequeItems[i].IsSelected, new { @id = "chk_" + Model.PrintChequeItems[i].DocumentNo, @class = "check", @onchange = "checkMe(this, '" + Model.PrintChequeItems[i].DocumentNo + "')" })
                                        </td>
                                        <td>
                                            <a id="lnkPrintToPdf_@Model.PrintChequeItems[i].DocumentNo" class="prnt" href="javascript:;" onclick="PrintChequeToPdf('@Model.PrintChequeItems[i].DocumentNo', '@Model.SelectedBranchCode');" disabled="disabled">Print Cheque to Pdf</a>
                                        </td>
                                    </tr>
                                    }
                                </tbody>

                            </table>
                        </div>

                        <div class="modal fade" id="confirm-save" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                            <div class="modal-dialog">
                                <div class="modal-content">

                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                        <h4 class="modal-title" id="myModalLabel">Confirmation</h4>
                                    </div>

                                    <div class="modal-body">
                                        <p>Cheques of selected documents have been printed. Tag them as saved.</p>
                                        <hr />
                                        <p>Do you wish to continue?</p>
                                    </div>

                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-primary" data-dismiss="modal">No</button>
                                        <button type="button" class="btn btn-primary btn-ok" data-dismiss="modal">Yes</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </fieldset>
            }
            </div>

        </div>
    </div>
</div>

@section Styles {
    @Styles.Render("~/Content/plugins/dataTables/dataTablesStyles")

    <style>

        .divScroll {
            height: 400px;
            overflow-x: scroll;
            border: solid 1px #EBEBEB;
        }

    select[disabled], select[disabled]:hover {
        pointer-events: none;
        color: #e1e1e1;
    }

    .divSearch {
        background-color: #F5F5F6;
        border: 1px solid #EBEBEB;
        margin-top: 5px;
    }

    .table-search {
        width: 100%;
        border: none;
    }

        .table-search td {
            margin: 10px 10px 10px 10px;
            padding: 10px 10px 10px 10px;
            vertical-align: bottom;
        }

        .table-search .tdAlignRight {
            text-align: right;
        }

        .table-search .tdFixed {
            width: 20%;
        }

            .table-search .tdFixed div {
                margin-bottom: 0px;
            }

        .table-search td .btnSearch {
            margin-bottom: 0px;
        }


        .table-search td span {
            margin-left: 7px;
        }
</style>



}

@section Scripts {
    @*@Scripts.Render("~/bundles/jqueryval")*@

    <script id="onDocumentReady" type="text/javascript">

        $(document).ready(function () {

            $(window).keydown(function (event) {
                if (event.keyCode == 13) {
                    Refresh();
                    return false;
                }
            });

            $('#confirm-save').on('click', '.btn-ok', function (e) {
                document.getElementById('btnSave').click();
            });

        });

    </script>

    <script id="methods" type="text/javascript">

        function Refresh() {
            document.getElementById('btnSearch').click();
        }

        function checkAll(me) {
            var val = $(me).is(':checked');

            $('.check').each(function () {
                $(this).prop('checked', val);
            });

            $('.prnt').each(function () {

                if (val) {
                    $(this).removeAttr('disabled');
                }
                else {
                    $(this).attr('disabled', 'disabled');
                }

            });


        }

        function checkMe(me, id) {
            var allIsChecked = true;

            $('.check').each(function () {
                var val = $(this).is(':checked');

                if (!val)
                    allIsChecked = false;

            });

            $('#chkCheckAll').prop('checked', allIsChecked);

            var meVal = $(me).is(':checked');
            if (meVal) {
                $('#lnkPrintToExcel_' + id).removeAttr('disabled');
                $('#lnkPrintToPdf_' + id).removeAttr('disabled');
            }
            else {
                $('#lnkPrintToExcel_' + id).attr('disabled', 'disabled');
                $('#lnkPrintToPdf_' + id).attr('disabled', 'disabled');
            }


        }

        function PrintChequeToPdf(documentNo, branchCode) {
            $.ajax({
                url: '/Funding/PrintChequeToPdf',
                type: "GET",
                dataType: "JSON",
                data: { documentNo: documentNo, branchCode: branchCode },
                success: function (print) { }
            });
        }

    </script>

    <script id="validations" type="text/javascript">

        function ValidateFields()
        {
            var hasSelected = false;
            var okFields = true;

            $('.trData').each(function () {

                var chk = this.children[10].children[0];
                var ischecked = $(chk).is(':checked');
                
                var txtChequeNo = this.children[8].children[0];
                var valChequeNo = this.children[8].children[1];

                var txtChequeAmount = this.children[9].children[0];
                var valChequeAmount = this.children[9].children[1];

                var okChequeNo = true;
                var okChequeAmount = true;

                if (ischecked) {
                    hasSelected = true;
                    okChequeNo = ValidateChequeNo(txtChequeNo.id, valChequeNo.id);
                    okChequeAmount = ValidateChequeAmount(txtChequeAmount.id, valChequeAmount.id);
                }
                else {
                    $(valChequeNo).hide();
                    $(valChequeAmount).hide();
                }
                
                if (!okChequeNo || !okChequeAmount) {
                    okFields = false;
                    return;
                }
            });

            if (hasSelected && okFields) {
                $('#btnSubmit').click();
            }
        }

        function ValidateChequeNo(chequeNoID, divValChequeNoID) {

            var bln = true;

            var val = $.trim($('#' + chequeNoID).val());

            if (val == '') {
                $('#' + divValChequeNoID).show();
                bln = false;
            }
            else {
                $('#' + divValChequeNoID).hide();
                bln = true;
            }

            return bln;
        }

        function ValidateChequeAmount(chequeAmountID, divValChequeAmountID)
        {
            var bln = true;
            var val = $.trim($('#' + chequeAmountID).val().replace(',',''));

            if (isNaN(parseFloat(val)))
            {
                $('#' + chequeAmountID).val("0.00");
                $('#' + divValChequeAmountID).show();
                bln = false;
            }
            else if (parseFloat(val) <= 0) {
                
                $('#' + divValChequeAmountID).show();
                bln = false;
            }
            else {
                $('#' + divValChequeAmountID).hide();
                bln = true;
            }

            return bln;

        }

        function FormatMe(me) {
            var val = $(me).val().replace(',', '');

            if (isNaN(parseFloat(val)))
                val = 0.00;

            var decimalFormat = parseFloat(val).toFixed(2);
            var commaFormat = FormatNumber(decimalFormat);

            $(me).val(commaFormat);
        }

        function FormatNumber(nStr) {
            nStr += '';
            var x = nStr.split('.');
            var x1 = x[0];
            var x2 = x.length > 1 ? '.' + x[1] : '';
            var rgx = /(\d+)(\d{3})/;
            while (rgx.test(x1)) {
                x1 = x1.replace(rgx, '$1' + ',' + '$2');
            }
            return x1 + x2;
        }


    </script>

}
